package com.changfeng.controller;

import com.changfeng.bean.dto.vo.*;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @PostMapping("/saveCommodity")
    public void saveCommodity(@RequestBody CommodityDetailsVO vo) {
        if (vo == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        commodityService.saveCommodity(vo);
    }

    @GetMapping("/getCommodityDetails")
    public CommodityDetailsVO getCommodityDetails(Integer commodityId) {
        if (commodityId == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        return commodityService.getCommodityDetails(commodityId);
    }

    @GetMapping("/findCommodityList")
    public List<CommodityListVO> findCommodityList(CommodityQueryVO vo) {
        if (vo == null) {
            vo = new CommodityQueryVO();
        }
        return commodityService.findCommodityList(vo);
    }

    @GetMapping("/deleteCommodity")
    public void deleteCommodity(Integer commodityId) {
        if (commodityId == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        commodityService.deleteCommodityById(commodityId);
    }

    @PostMapping("/toOrder")
    public List<CommodityConfirmVO> toOrder(@RequestBody List<Integer> ids) {
        return commodityService.findByIds(ids);
    }

}
